package com.irdstudio.efp.cus.api.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.irdstudio.basic.framework.core.constant.Sex;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.cus.service.facade.CusIndivService;
import com.irdstudio.efp.cus.service.vo.CusIndivVO;
import com.irdstudio.efp.cus.service.vo.WriteCusIndivModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 需求编号：【2019D0509】（业务提的需求编号）
 * 问题编号：【】（若不是因为修复问题而新增该类，则留空）
 * 开发人员：【wanglh4】
 * 创建日期：【2020/1/20 18:50】
 * 功能描述：
 */
@Component
@Async
public class AsyncExportTask {

    @Autowired
    @Qualifier("cusIndivService")
    private CusIndivService cusIndivService;

    @Value("${PAGESIZE}")
    private int pagesize;

    public void exportTask(CusIndivVO vo, String fileName, String filePath, int count) {
        try {
            //计算查询次数
            int loopTimes = count / pagesize;
            int remainCount = count % pagesize;
            if (remainCount != 0) loopTimes += 1;
            //设置分页信息
            vo.setSize(pagesize);
            vo.setTotal(count);
            //导出数据
            ExcelWriter writer = EasyExcel.write(filePath, WriteCusIndivModel.class).build();
            for (int i = 0; i < loopTimes; i++) {
                vo.setPage(i + 1);
                List<CusIndivVO> writeCusIndivList = cusIndivService.queryExAllOwner(vo);
                List<WriteCusIndivModel> writeModelList = copyBeansVal(writeCusIndivList);
                if (Objects.nonNull(writeModelList) && !writeModelList.isEmpty()) {
                    //导出数据
                    WriteSheet writeSheet = EasyExcel.writerSheet(i,fileName + i).build();
                    writer.write(writeModelList, writeSheet);
                }
            }
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<WriteCusIndivModel> copyBeansVal(List<CusIndivVO> writeCusIndivList) {
        List<WriteCusIndivModel> writeModelList = new ArrayList<>();
        writeCusIndivList.forEach(item->{
            WriteCusIndivModel writeCusIndivModel = new WriteCusIndivModel();
            writeCusIndivModel.setCertCode(item.getCertCode());
            writeCusIndivModel.setCertType(item.getCertType());
            writeCusIndivModel.setCreateTime(TimeUtil.getCurrentDateTime());
            writeCusIndivModel.setCusId(item.getCusId());
            writeCusIndivModel.setCusName(item.getCusName());
            writeCusIndivModel.setFstAppChannel(item.getFstAppChannel());
            writeCusIndivModel.setIndivSex(Sex.MALE.getEnname().equals(item.getIndivSex())?Sex.MALE.getCnname():Sex.FEMALE.getCnname());
            writeCusIndivModel.setPhone(item.getPhone());
            writeModelList.add(writeCusIndivModel);
        });
        return writeModelList;
    }
}
